const app = getApp();
import HttpRequst from '../../utils/httpRequst.js'
import API from '../../utils/config.js'
import WxValidate from '../../utils/WxValidate'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    intervalTime:60,//验证码倒计时的时间
    isInterval:false,//是否在验证码获取期间
    mobile:"",
    Timer:"",
    isMobileValid:false,
    validateTipIsShow: false,
    validateTipMsg: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._initValidate();
  },
  formSubmit: function (e) {
    const params = e.detail.value;
    var that = this;
    if (!this.WxValidate.checkForm(e)) {
      const error = this.WxValidate.errorList[0];
      this.setData({
        validateTipMsg: error.msg,
        validateTipIsShow: true
      })
      this._validateTipHide();
    } else {
       //HttpRequst(loading,url,发送header类型, sessionId, params,method, ask, function (res){})
      HttpRequst(true, API.login,1,"", params,"POST",false, function (res) {
          if(!!res.code){
            app.globalData.userInfo = res.data;
            wx.setStorage({
              key: "userinfo",
              data: res.data
            });
           
            wx.showLoading({
              title: '登录成功',
              success:function(){
                wx.showToast({
                  title: '成功',
                  icon: 'success',
                  duration: 1000,
                })
                setTimeout(function(){
                  wx.redirectTo({
                    url: '/pages/index/index'
                  })
                },1000);
              }	
            })
            
          }else{
            that.setData({
              validateTipMsg: res.msg,
              validateTipIsShow: true
            })
          }
      }) 
    }

  },
  getCode:function(e){
    var that=this;
    
    wx.request({
      url: API.baseUrl+API.getCode, // 
      method:"POST",
      data: {
        mobile: this.data.mobile
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success(res) {
        
        if(res.data.code==400){
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          });
          that.setData({
            isInterval: false
          })
        }else{
          that.setData({
            isInterval: true
          })
          that._countdownYzm();
        }
         
      }
    })

    
    
    
    
  },
 /**
   * 校验手机，决定是否可以获取手机短信验证码
   */
  mobileInput:function(e){
    this.setData({
      mobile: e.detail.value,
      isMobileValid: /^1[3456789]\d{9}$/.test(e.detail.value)
    })
  },
  /**
   * 60秒后从新获取
   */
  _countdownYzm: function () {
    var that = this;
    var Timer = setInterval(function () {
      if (that.data.intervalTime <= 1) {
        clearInterval(Timer);
        that.setData({
          isInterval: false,
          intervalTime: 60
        });
      }else{
        that.setData({
          intervalTime: that.data.intervalTime - 1
        });
      }
    }, 1000)
  },
  /**
   * 2秒后隐藏错误提示信息
   */
  _validateTipHide: function () {
    var that = this;
    setTimeout(function () {
      that.setData({
        validateTipIsShow: false
      })
    }, 2000);
  },
  /**
   * 初始化验证插件
   */
  _initValidate: function () {
    const rules = {
      mobile: {
        required: true,
        tel: true
      },
      code: {
        required: true
      }
    };
    const messages = {
      mobile: {
        required: "请输入您的手机号码",
        tel: "请输入正确的手机号码"
      },
      code: {
        required: "请输入短信验证码"
      }
    };
    this.WxValidate = new WxValidate(rules, messages);
  }
})